#include "gasdetection.h"
#include "hi_adc.h"
#include "iot_errno.h"

#define GAS_SENSOR_CHAN_NAME 2

float ConvertToVoltage(unsigned short data)
{
    return (float)data * 1.8 * 4 / 4096;
}

void Gasdetection(float *gas)
{
    short data = 0;
    int ret;
    ret = hi_adc_read(GAS_SENSOR_CHAN_NAME, &data, HI_ADC_EQU_MODEL_4, HI_ADC_CUR_BAIS_DEFAULT, 0);
    if (ret == IOT_SUCCESS) {
        float Vx = ConvertToVoltage(data);
        *gas = 5 / Vx - 1;
			 printf("\r\n hi_adc_read ok, data=%d, vx=%f, data_gas=%f", data, Vx, *gas);
        } else {
			printf("\r\n hi_adc_read fail, ret=%d", ret);
		}
    return;
}